package com.wb3.organizer.action;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.service.AccountService;

public class AccountAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//初始化日志对象
	private static Logger log = Logger.getLogger(AccountAction.class);

	private Map<String, Object> session;

	private Account account;

	private String repassword;

	private AccountService service;

	private String msg;

	private String operate;
	
	private String theme;

	public void setSession(Map<String, Object> inSession) {
		this.session = inSession;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public AccountService getService() {
		return service;
	}

	public void setService(AccountService service) {
		this.service = service;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}
	
	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	
	public String create() throws CloneNotSupportedException {

		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AccountAction.create()...\n");

		switch (service.accountCreate(account, repassword)) {
		case 0:
			msg = "添加用户成功!!";
			//创建成功,将用户保存在session中
			session.put("account", account.clone());
			log.debug("User successfully added!");
			return SUCCESS;
		case 1:
			msg = "两次输入的密码不一致!!";
			log.debug("The two passwords you entered did not match");
			return ERROR;
		case 2:
			msg = "用户名已经存在!!";
			log.debug("Username already exists");
			return ERROR;
		}
		return ERROR;
	}

	public String update() throws Exception {

		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AccountAction.update()...\n");

		Account sessionAcc = (Account) session.get("account");
		account.setAcc_id(sessionAcc.getAcc_id());
		switch (service.accountUpdate(account, sessionAcc, repassword)) {
		case 1:
			msg = "两次输入的密码不一致!!!";
			return ERROR;
		case 0:
			msg = "修改成功!!!";
			
			session.put("account", sessionAcc.clone());
			this.setOperate("accountUpdateOK");
			return SUCCESS;
		}
		return ERROR;
	}

	public String delete() {

		log.info("\n\n----------------------------------------------------------\n");
		log.debug("AccountDelete.delete()...\n");

		return ERROR;
	}
	
	
	public String theme() throws Exception {
		Account account = (Account) session.get("account");
		account.setTheme(Integer.parseInt(theme));
		service.accountTheme(account);
		session.put("account", account.clone());
		
		this.setMsg("主题修改成功!!!");
		this.setOperate("accountThemeOK");
		return SUCCESS;
	}

}
